package com.yycwxt.modules.admin.store;

import com.yycwxt.common.ReturnResult;
import com.yycwxt.domain.Store;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@Api("仓库相关接口")
@RestController
@Validated
@RequestMapping("/store")
public class StoreController {

    private final StoreService storeService;

    // MP
    private final StoreService2 storeService2;

    public StoreController(@Autowired StoreService storeService, @Autowired StoreService2 storeService2) {
        this.storeService = storeService;
        this.storeService2 = storeService2;
    }


    @GetMapping("/list")      // @GetMapping()
    @ApiOperation("查询全部仓库")
    public ReturnResult selectAll() {
        return ReturnResult.buildSuccessResult(storeService.selectAll());
    }

    @GetMapping("/search")   // @GetMapping("/{input}")
    @ApiOperation("根据输入条件模糊查询仓库")
    public ReturnResult selectByInput(@ApiParam(name = "input", required = true, type = "String") @Validated @NotNull(message = "不能为空") String input) {
        return ReturnResult.buildSuccessResult(storeService.selectByInput(input));
    }

    @GetMapping("/search2")
    public ReturnResult selectByInput2(String input) {
        return ReturnResult.buildSuccessResult(storeService.selectByInput2(input));
    }

    @GetMapping("/delete/{id}") // @DeleteMapping("/{id}")
    @ApiOperation("根据id删除相应的仓库")
    public ReturnResult deleteById(@ApiParam(name = "id", required = true, type = "Integer") @PathVariable Integer id) {
        try {
            if (storeService2.removeById(id)) {
                return ReturnResult.buildSuccessResult(true);
            } else {
                return ReturnResult.buildFailureResult("id可能不存在", false);
            }
        } catch (Exception e) {
            return ReturnResult.buildFailureResult("id必须为数字", false);
        }
    }

    @PostMapping("/update")      // PutMapping("/{id}")
    @ApiOperation("根据id修改相应仓库")
    public ReturnResult updateById(@ApiParam(name = "Store对象", required = true, type = "Store") @RequestBody @Validated Store store) {
        try {
            if (storeService2.updateById(store)) {
                return ReturnResult.buildSuccessResult(true);
            } else {
                return ReturnResult.buildFailureResult("id可能不存在或者name重复", false);
            }
        } catch (DuplicateKeyException dke) {
            return ReturnResult.buildFailureResult("name 值可能重复", false);
        } catch (Exception e) {
            return ReturnResult.buildFailureResult("参数错误");
        }
    }

    //@PostMapping("/upload")
    //@ApiOperation("Excel导入数据")
    //public ReturnResult excelToDatabase(@ApiParam(value = "上传文件", type = "_file") @RequestPart("file") MultipartFile file) {
    //    try {
    //        if (!file.isEmpty()) {
    //            if (storeService2.importFileToDB(true, file)) {
    //                return ReturnResult.buildSuccessResult("导入成功", null);
    //            } else {
    //                return ReturnResult.buildFailureResult("导入失败", null);
    //            }
    //        } else {
    //            return ReturnResult.buildFailureResult("文件为空", null);
    //        }
    //    }
    //    catch (Exception e){
    //        return ReturnResult.buildFailureResult("不支持此文件类型 或 文件内容格式不对", null);
    //    }
    //}

    @PostMapping("/add")
    @ApiOperation("新增仓库")
    public ReturnResult addStore(@ApiParam(name = "Store对象", required = true, type = "Store") @RequestBody @Validated Store store) {
        try {
            return ReturnResult.buildSuccessResult(storeService2.save(store));
        } catch (DuplicateKeyException dke) {
            return ReturnResult.buildFailureResult("id/name 值重复");
        } catch (Exception e) {
            return ReturnResult.buildFailureResult("参数错误");
        }
    }
}
